<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : AdminConfig.php
 * Created on : 2021-1-31 15:06:32
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\model;

use think\Model;
use app\admin\model\AdminConfigTab;

/**
 * 系统配置
 * Class AdminConfig
 * Tiger <1192851302@qq.com> 2021-1-31 15:06:32
 */
class AdminConfig extends Model {

    public static $typeList = [
        'text' => '文本输入框',
        'password' => '密码输入框',
        'number' => '数字输入框',
        'select' => '下拉框',
        'checkbox' => '多选框',
        'radio' => '单选框',
        'textarea' => '多行文本域',
        'year' => '年份选择',
        'month' => '月份选择',
        'date' => '日期选择',
        'time' => '时间选择',
        'datetime' => '日期时间',
        'upload' => '本地上传'
    ];

    /**
     * 获取数据列表-分页
     * @author Tiger <1192851302@qq.com>
     * @param array $where
     * @return array
     */
    public static function getList($where) {
        $model = new self;
        if (!empty($where['title'])) {
            $model = $model->where('title', 'LIKE', '%' . $where['title'] . '%');
            unset($where['title']);
        }
        if (input('limit', 0) <= 0) {
            $data = $model->where($where)->order('tab_id DESC, sort ASC')->select()->toArray();
            foreach ($data as &$item) {
                $item = self::_bind_data($item);
            }
            return $data;
        }
        $models = $model->where($where)->withoutField(['value'])->order('tab_id DESC, sort ASC')->paginate(input('limit', 10))->each(function($item) {
            return self::_bind_data($item);
        });
        return [
            'code' => 0,
            'items' => $models->items(),
            'total' => $models->total()
        ];
    }

    /**
     * 绑定数组
     * @authr Tiger <1192851302@qq.com>
     * @param type $item
     * @return type
     */
    public static function _bind_data($item) {
        $item['typeName'] = self::$typeList[$item['type']];
        $item['tabName'] = AdminConfigTab::where('id', $item['tab_id'])->value('title');
        $item['value'] = json_decode($item['value'], true);
        return $item;
    }

    /**
     * 修改单个配置
     * @authr Tiger <1192851302@qq.com>
     * @param $key
     * @param $value
     * @return bool
     */
    public static function setValue($key, $value) {
        //校验？
        $rules = self::where('name', $key)->value('rules');
        if ($rules) {
            $array = explode('|', str_replace(['required', 'phone', 'identity'], ['require', 'mobile', 'idCard'], $rules));
            $vali = '';
            foreach ($array as $rule) {
                if (in_array($rule, ['require', 'mobile', 'idCard', 'email', 'url', 'number', 'date'])) {
                    $vali .= $rule . '|';
                    //$message[$rule] = '';
                }
            }
            if ($vali) {
                $validate = validate([$key => substr($vali, 0, -1)]);
                if ($validate->check([$key => $value]) === false) {
                    exception($validate->getError());
                }
            } else {
                //其他格式
            }
        }
        $bool = self::update(['value' => json_encode($value)], ['name' => $key]);
        return $bool;
    }

    /**
     * 获取单个参数配置
     * @authr Tiger <1192851302@qq.com>
     * @param $key
     * @return bool|mixed
     */
    public static function getOne($key) {
        if (empty($key) || !($config_one = self::where('name', $key)->find())) {
            return false;
        }
        return json_decode($config_one['value'], true);
    }

    /**
     * 获得多个参数
     * @authr Tiger <1192851302@qq.com>
     * @param $keys
     * @return array
     */
    public static function getMore(array $keys) {
        $list = self::whereIn('name', $keys)->column('value', 'name') ?: [];
        foreach ($list as $key => $value) {
            $list[$key] = json_decode($value, true);
        }
        return $list;
    }

}
